-- guildMemberVo
-- Author: mm
-- Date: 2018-03-19 16:04:29
-- 公会成员数据结构

local UserBaseInfo = require("app.public.dao.UserBaseInfo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

-- message S2C_GuildMember {
-- 	base.UserBaseInfo userBaseInfo = 1;
-- 	int32 position = 2;		// 1 会长, 2 精英, 3 普通
-- 	int32 offlineStamp = 3; // 离线时间戳，为0表示在线
-- 	int32 donateLv = 4;     // 捐献等级
-- 	int32 donateCopies = 5; // 捐赠份数
-- 	int32 donateTimes = 6;  // 今日已捐赠次数
-- 	int32 joinStamp = 7;    // 入会时间戳
-- }
function ClassRef:ctor(data)
	self._memberUserBaseInfo = UserBaseInfo.new()
	self._position = 0
	self._offlineStamp = -1
	self._donateLv = 0
	self._donateCopies = 0
	self._donateTimes = 0
	self._joinStamp = 0
	self._titleId = 0 --官职ID
	self._haveGetFirstDonateReward = false --是否已领过每日首次捐献奖励

	if data then
		self:read(data)
	end

	-- 可以通过 . 访问基础信息
	setmetatable(self, {
		__index = function(tab, key)
			local ret = rawget(tab, key)
			ret = rawget(tab.class, key)
			if not ret and tab["_memberUserBaseInfo"] then
				ret = tab["_memberUserBaseInfo"][key]
			end
			return ret
		end
	})
end

function ClassRef:read(data)
	self._memberUserBaseInfo = UserBaseInfo.new( data.userBaseInfo )
	self._position = data.position
	self._offlineStamp = data.offlineStamp
	self._donateLv = data.donateLv
	self._donateCopies = data.donateCopies
	self._donateTimes = data.donateTimes
	self._joinStamp = data.joinStamp
	self._titleId = data.titleId or 0--官职ID

	self._memberUserBaseInfo:setOnline( 0 == self._offlineStamp )
	self._haveGetFirstDonateReward = data.firstDonateExtraRewardRecord
	self._posDB = GD:queryGuildPosById( self._position )
	assert( self._posDB, " ---------------------->>> guildMemberVo posDB cant be nil" )
end

-- 返回 userbaseInfo 结构
function ClassRef:getMemberInfo()
	return self._memberUserBaseInfo
end
function ClassRef:getMemberName()
	return self._memberUserBaseInfo.name
end
function ClassRef:getMemberId()
	return self._memberUserBaseInfo.uid
end
function ClassRef:getMemberLevel()
	return self._memberUserBaseInfo.level
end
function ClassRef:getMemberCombatPower()
	return self._memberUserBaseInfo.combatPower
end

--设置官职ID
function ClassRef:setTitleId(id)
	self._titleId = id
end

--返回官职ID
function ClassRef:getTitleId()
	return self._titleId
end

--判断是否满足最小的官职 1000 - 1999 以内是一类 2000 - 2999是一类
function ClassRef:isEnoughTitle(titleId)
	if titleId < 2000 then
		if self._titleId >= 1000 and self._titleId < 2000 then
			return self._titleId >= titleId
		end
	else
		if self._titleId >= 2000 then
			return self._titleId >= titleId
		end
	end

	return false
end

-- 是否是"我"？
function ClassRef:isMe()
	return self._memberUserBaseInfo.uid == UD.player.uid
end

-- 是否在线？
function ClassRef:isOnline()
	return 0 == self._offlineStamp
end

-- 离线时间
function ClassRef:offlineStr()
	local offlineStr = ""
	if 0 < self._offlineStamp then -- 离线状态？
		local curTime = math.floor(app.session:getServerTime() or app:getTime())
		--type=2
		--      >1天 return {day},gap
		--      <=1天 return {h,m,s},1
		local params, interval = TimeUtil.secondsToStr( ( curTime - self._offlineStamp ) , 2 )
		if #params == 1 then -- 超过一天？
			local timeList = TimeUtil.secondsToStr(interval, 3)
			offlineStr = string.format( L("offline_state1"), params[1], timeList[1] )
		else
			local hour = params[1]
			if 0 >= hour then
				offlineStr = L("offline_state3")
			else
				offlineStr = string.format( L("offline_state2"), hour )
			end
		end
	end
	return offlineStr
end

--获得下线的天数
function ClassRef:getOfflineDay()
	local curTime = math.floor(app.session:getServerTime() or app:getTime())
	local params, interval = TimeUtil.secondsToStr( ( curTime - self._offlineStamp ) , 2 )

	if #params == 1 then
		return tonumber(params[1])
	else
		return 0
	end
end

-- 返回地位
function ClassRef:getPosition()
	return self._position
end

-- 是否是会长
function ClassRef:isGuildLeader()
	return 1 == self._position
end
-- 是否是精英
function ClassRef:isGuildOfficer()
	return 2 == self._position
end

--是普通成员
function ClassRef:isNormalMember()
	return 3 == self._position
end

--是否拥有公会的管理权限
function ClassRef:isGuildManager()
	if self:isGuildLeader() or self:isGuildOfficer() then
		return true
	end
	local guildModel = app:getInst("NewguildModel")
	--若是最高可授予官职 也是有管理权限的
	return guildModel:judgePeerageIsGuildManager(guildModel:getMaxActiveTerritoryId(),self._titleId)
end


-- 返回地位对应的图标
function ClassRef:getPositionIcon()
	if self._titleId == 0 then
		return GD:getImagePath( Res.guildPositionIconPath, self._posDB.positionIcon )
	else
		local peerageInfo = GD:queryPeerageById(self._titleId)
		assert(peerageInfo ~= nil, "peerageInfo must not nil and the titleId is "..self._titleId)
		return GD:getImagePath( Res.guildPositionIconPath, peerageInfo.icon )
	end
end

-- 捐献等级
function ClassRef:getDonateLv()
	return self._donateLv
end

-- 捐献份数
function ClassRef:getDonateCopies()
	return self._donateCopies
end

-- 今日已经捐献过的次数
function ClassRef:getDonatedTimes()
	return self._donateTimes
end

function ClassRef:isHaveGetFirstDonateReward()
	return self._haveGetFirstDonateReward
end

-- 返回入会时间戳
function ClassRef:getJoinedTimes()
	return self._joinStamp
end

return ClassRef
